﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;
using System.Text.RegularExpressions;

namespace LTJ.Web.UI
{
    /// <summary>
    /// 基础用户控件
    /// </summary>
    public class BaseWebControl : UserControl
    {
        /// <summary>
        /// 页面大小
        /// </summary>
        protected internal int PageSize = 10;
        /// <summary>
        /// 页码
        /// </summary>
        protected internal int PageIndex = SASRequest.GetInt("currentPage", 1);
        /// <summary>
        /// 分页
        /// </summary>
        protected internal string PageListStr = "";
        /// <summary>
        /// 总行数
        /// </summary>
        protected internal int RowsCount = 0;
        /// <summary>
        /// 条件语句
        /// </summary>
        protected internal string ConditionStr = "";
        /// <summary>
        /// 前台脚本
        /// </summary>
        protected internal string ScriptStr = "";
        /// <summary>
        /// 用户实体
        /// </summary>
        protected internal UserInfo uInfo = new UserInfo();
        /// <summary>
        /// 用户ID
        /// </summary>
        protected internal int userid = 0;
        /// <summary>
        /// 用户名
        /// </summary>
        protected internal string username = "";
        /// <summary>
        /// 用户企业ID
        /// </summary>
        protected internal int compid = 0;
        /// <summary>
        /// 用户组
        /// </summary>
        protected internal int groupid = 0;
        /// <summary>
        /// 配置文件
        /// </summary>
        protected internal GeneralConfigInfo config = GeneralConfigs.GetConfig();
        /// <summary>
        /// 控件文件名
        /// </summary>
        protected string controllername = "";
        /// <summary>
        /// 刷新展示层
        /// </summary>
        protected string showdiv = "showdata";
        /// <summary>
        /// 数据存储层
        /// </summary>
        protected string showhid = "";
        /// <summary>
        /// 数据调用对象
        /// </summary>
        protected DataTable dataDT = new DataTable();
        /// <summary>
        /// 企业信息实体
        /// </summary>
        protected CompanyInfo compInfo = new CompanyInfo();
        /// <summary>
        /// 权限信息实体
        /// </summary>
        protected Authority authInfo = new Authority();

        public BaseWebControl()
        {
            controllername = this.GetType().Name.Replace("_ascx", ".ascx").Replace("datacontrol_", "");
            userid = Utils.StrToInt(FrameWorkUtil.GetCookie("userid"), -1);
            string pagename = SASRequest.GetPageName();

            if (userid > 0)
            {
                username = FrameWorkUtil.GetCookie("username");
                groupid = Utils.StrToInt(FrameWorkUtil.GetCookie("usergid"), -1);
                compid = Utils.StrToInt(FrameWorkUtil.GetCookie("usercid"), -1);
                if (groupid == Convert.ToInt16(ManageGroup.AdminGroup)) compid = 0;
            }

            ShowUserControl();
        }

        /// <summary>
        /// 返回用户信息
        /// </summary>
        protected void GetUser()
        {
            uInfo = Users.GetUser(userid);
        }
        /// <summary>
        /// 实例化用户权限
        /// </summary>
        protected void GetAuth()
        {
            authInfo = Users.GetAuthInfoCache(compid, userid);
        }
        /// <summary>
        /// 实例化企业信息
        /// </summary>
        protected void GetComp()
        {
            compInfo = Companys.GetCompanyByID(compid);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        protected string UserLogin(string lname, string lpwd)
        {
            userid = LTJ.FrameWork.Users.CheckPassword(lname, lpwd, true);
            uInfo = Users.GetUser(userid);

            if (uInfo == null || uInfo.id <= 0)
            {
                return "账号或密码错误！";
            }
            else
            {
                FrameWorkUtil.WriteDYJUserCookies(uInfo);
                OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
                OpLogs.CreateCompanyUserLoginLog(uInfo.id, uInfo.lname, uInfo.gid, uInfo.cid, SASRequest.GetIP());
                Context.Response.Redirect(config.Weburl + "index.aspx");
                //登录用户类型为管理员
                //if (uInfo.gid == 2)
                //{
                //    Context.Response.Redirect("~/admin/usergroup_index.aspx");
                //}
                ////登录用户类型为VIP
                //else if (uInfo.gid == 20)
                //{
                //    Context.Response.Redirect("~/user/userindex.aspx");
                //}
                //else
                //{
                //    Context.Response.Redirect("~/index.aspx");
                //}

                return "";
            }
        }

        protected void AddScriptStr(string scriptstrs)
        {
            ScriptStr += "<script type=\"text/javascript\">\r\n";
            ScriptStr += scriptstrs + "\r\n";
            ScriptStr += "</script>\r\n";
        }

        /// <summary>
        /// 操作动作判断
        /// </summary>
        public bool OpAction(OprateAction aotype)
        {
            string pagename = SASRequest.GetString("pagename");
            if (pagename == "") pagename = SASRequest.GetPageName();
            Regex objStr = new Regex(@"," + pagename + @"\|([\s\S!,]+?)\|\d\,", RegexOptions.None);
            Match m = objStr.Match("," + compInfo.Authoritys);
            if (!m.Success) return false;
            Match sm = objStr.Match("," + authInfo.Authoritys);
            if (!m.Success) return false;
            if (!m.Groups[0].Value.Contains("|" + aotype + "|")) return false;
            if (!sm.Groups[0].Value.Contains("|" + aotype + "|")) return false;
            return true;
        }

        /// <summary>
        /// 控件调用展示
        /// </summary>
        public virtual void ShowUserControl()
        {
            return;
        }
        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="parmstr">分页参数集合</param>
        public void GetPageList(string parmstr)
        {
            if (SASRequest.GetInt("postnumber", 0) > 0)
            {
                PageListStr = LTJ.Common.Utils.LuffyPage3(RowsCount, PageSize, PageIndex, controllername, "postnumber=" + SASRequest.GetInt("postnumber", 0), showdiv);
            }
            else
            {
                PageListStr = LTJ.Common.Utils.LuffyPage3(RowsCount, PageSize, PageIndex, controllername, parmstr, showdiv);
            }
        }
    }
}
